﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using MySql.Data.MySqlClient;
using EnvironmentManagerSystem.MODE;

namespace EnvironmentManagerSystem.BLL
{
    public class Role
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public static RoleDataCollection GetList()
        {
            try
            {
                RoleDataCollection list = new RoleDataCollection();
                string strSql = @"SELECT * FROM `TBS001_ROLE` WHERE INUSE=1";
                using (MySqlDataReader dr = DbHelperMySQL.ExecuteReader(strSql))
                {
                    while (dr.Read())
                    {
                        RoleData model = new RoleData();
                        model.ID = (int)dr["ID"];
                        model.RoleName = dr["RoleName"].ToString();
                        model.InUse = (int)dr["InUse"];
                        list.Add(model);
                    }
                }
                return list;
            }
            catch
            {
                return null;
            }
        }
        #region (x)
        //获取角色列表
        public static RoleDataCollection GetLists()
        {
            try
            {
                RoleDataCollection list = new RoleDataCollection();
                string strSql = @"SELECT * FROM `TBS001_ROLE` WHERE INUSE=2";
                using (MySqlDataReader dr = DbHelperMySQL.ExecuteReader(strSql))
                {
                    while (dr.Read())
                    {
                        RoleData model = new RoleData();
                        model.ID = (int)dr["ID"];
                        model.RoleName = dr["RoleName"].ToString();
                        model.InUse = (int)dr["InUse"];
                        list.Add(model);
                    }
                }
                return list;
            }
            catch
            {
                return null;
            }
        }
        //添加新角色
        public static bool AddRoles(string ROLENAME)
        {
            try
            {
                string strSql = string.Format(@"INSERT INTO `TBS001_ROLE` (ROLENAME,INUSE) VALUES ('{0}',2)", ROLENAME);
                return DbHelperMySQL.ExecuteSql(strSql) > 0 ? true : false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 添加新角色
        /// </summary>
        /// <param name="ROLENAME">角色名</param>
        /// <returns></returns>
        public static bool AddRole(string ROLENAME)
        {
            try
            {
                string strSql = string.Format(@"INSERT INTO `TBS001_ROLE` (ROLENAME,INUSE) VALUES ('{0}',1)", ROLENAME);
                return DbHelperMySQL.ExecuteSql(strSql) > 0 ? true : false;
            }
            catch
            {
                return false;
            }
        }

      /// <summary>
      /// 修改角色
      /// </summary>
      /// <param name="model"></param>
      /// <returns></returns>
        public static bool UpdateRole(RoleData model)
        {
            try
            {
                string strSql = string.Format(@"update `TBS001_ROLE` set rolename='{0}' where ID={1}", model.RoleName, model.ID);
                return DbHelperMySQL.ExecuteSql(strSql) > 0 ? true : false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="ID">角色编号</param>
        /// <returns></returns>
        public static bool DeleteRole(int ID)
        {
            try
            {
                string strSql = string.Format(@"delete from `TBS001_ROLE` where ID={0}", ID);
                return DbHelperMySQL.ExecuteSql(strSql) > 0 ? true : false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public static bool GetQ(int ID,string funid)
        {
            try
            {
                bool b = false;
                string str = User.GetRoleID(ID);
                str = str.PadLeft(str.Length + 1, '\'');
                str = str.PadRight(str.Length + 1, '\'');
                str = str.Replace(",", "\',\'");
                string strSql = string.Format(@"SELECT b.* FROM tbs001_q b,tbs001_role c WHERE b.roleid = c.id AND c.rolename IN ({0}) AND b.funid = '{1}'", str, funid);
                using (MySqlDataReader dr = DbHelperMySQL.ExecuteReader(strSql))
                {
                    if (dr.HasRows)
                        return true;
                    else
                        return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
